package com.util;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @author haoya_Coding_one
 * @version 1.0
 * @create 2018/12/19 18:48
 * @description TODO 时间处理工具类
 */
public class DateUtil {
    private static Logger logger= LoggerFactory.getLogger(DateUtil.class);
    public static final DateFormat formatStandard=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final DateFormat formatAllNumber=new SimpleDateFormat("yyyyMMddHHmmssSSS");
    public static final DateFormat formatSimpleAllNumber=new SimpleDateFormat("yyyyMMddHHmmss");
    public static final DateFormat formatSimpleNumber=new SimpleDateFormat("yyyyMMdd");
    public static final DateFormat formatOnlyDate=new SimpleDateFormat("yyyy-MM-dd");
    //：我们在做时间格式转换时，主要是找对匹配时间格式的模式；
    // 另外，英文格式的时间转换时需要带上Locale.ENGLISH，否则会转换失败，
    // 因为它默认的是本地化的设置，除非你的操作系统是英文的，总之时间转换时需要时间格式与模式保持一致
    public static final DateFormat week3EnFormat = new SimpleDateFormat("E", Locale.ENGLISH);

    public static String formatCurrentTimeStandard(){
        return formatStandard.format(new Date());
    }

    public static String formatStandard(Date date){
        return null==date?"null":formatStandard.format(date);
    }

    public static Date parseStandard(String str){
        try {
            //等于null或者长度等于0
            return StringUtils.isBlank(str)?null:formatStandard.parse(str);
        } catch (ParseException e) {
            logger.error("date parse error, source=["+str+"], error message see:\n"+e.getMessage());
        }
        return null;
    }


    /**
     * yyyyMMddHHmmssSSS
     * @param date
     * @return
     */
    public static String formatAllNumber(Date date){
        return null==date?"null":formatAllNumber.format(date);
    }

    /**
     * yyyyMMdd
     * @param date
     * @return
     */
    public static String formatSimpleNumber(Date date){
        return null==date?"null":formatSimpleNumber.format(date);
    }

    /**
     *  "yyyyMMddHHmmss" Date
     * @param str
     * @return
     */
    public static Date parseSimpleAllNumber(String str){
        try {
            return StringUtils.isBlank(str)?null:formatSimpleAllNumber.parse(str);
        } catch (ParseException e) {
            logger.error("date parse error, source=["+str+"], error message see:\n"+e.getMessage());
        }
        return null;
    }

    /**
     * "yyyyMMddHHmmss"
     * @param date
     * @return
     */
    public static String formatSimpleAllNumber(Date date){
        return null==date?"null":formatSimpleAllNumber.format(date);
    }




    /**
     * 字符串转日期 Date yyyy-MM-dd
     * @param str
     * @return
     */
    public static Date parseOnlyDate(String str){
        try {
            return StringUtils.isBlank(str)?null:formatOnlyDate.parse(str);
        } catch (ParseException e) {
            logger.error("date parse error, source=["+str+"], error message see:\n"+e.getMessage());
        }
        return null;
    }


    /**
     * 格式化日期到"yyyy-MM-dd
     * @param date
     * @return
     */
    public static String formatOnlyDate(Date date){
        return date==null?null:formatOnlyDate.format(date);
    }


    /**
     * 添加小时
     * @param date
     * @param step
     * @return
     */
    public static Date addHour(Date date, int step){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, step);
        return calendar.getTime();
    }


    /**
     * 添加秒
     * @param date
     * @param step
     * @return
     */
    public static Date addSecond(Date date, int step){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, step);
        return calendar.getTime();
    }



    /**
     * 添加天数step
     * @param date
     * @param step
     * @return
     */
    public static Date addDay(Date date, int step){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, step);
        return calendar.getTime();
    }


    /**
     * 获得一个月的多少天offset
     * @param baseDate
     * @param offset
     * @return
     */
    public static Date getZeroOfSomeDay(Date baseDate, int offset){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(baseDate);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.add(Calendar.DAY_OF_MONTH, offset);
        return calendar.getTime();
    }

    /**
     * 获得一个月的第一天
     * @param baseDate
     * @return
     */
    public static Date getFirstDayOfMonth(Date baseDate){
        if (baseDate == null){
            baseDate = new Date();
        }
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(baseDate);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    /**
     *  //相差多少天
     * @param latter
     * @param former
     * @return
     */
    public static Integer diffDays(Date latter, Date former){
        if (latter == null || former == null){
            return null;
        }
        int divisor = 86400;    //60*60*24
        return (int)((latter.getTime() - former.getTime())%divisor);
    }

    public static String getWeek3En(Date date){
        return date==null?null:week3EnFormat.format(date).toLowerCase();//toLowerCase() 方法用于把字符串转换为小写。
    }
}
